package com.atguigu.boot;

import ch.qos.logback.core.db.DBHelper;
import com.atguigu.boot.bean.Pet;
import com.atguigu.boot.bean.User;
import com.atguigu.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author gcq
 * @Create 2020-12-26
 */
@SpringBootApplication
public class MyApplicationContext {
    public static void main(String[] args) {
        // 1、返回我们的IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MyApplicationContext.class, args);

        // 2、查看容器里面的组件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
        // 3、从容器中获取组件

//        User user01 = run.getBean("user01",User.class);
//        User user02 = run.getBean("user01",User.class);
//        System.out.println("组件:" + (user01 == user02) );

        // 4、配置类也是一个组件
//        MyConfig bean = run.getBean(MyConfig.class);
//        System.out.println("配置类:" + bean);
//        // 如果@Configuration(proxyBeanMethods =true) 代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有
//        // 保持组件单实例
//        User user = bean.user01();
//        User user1 = bean.user01();
//        System.out.println(user == user1);
//
//
//        User user01 = run.getBean("user01",User.class);
//        Pet pet = run.getBean("pet01",Pet.class);
//        System.out.println("user组件中的pet：" + (user01.getPet() == pet));
//
//
//        String[] beanNamesForType = run.getBeanNamesForType(User.class);
//        System.out.println("====");
//        for (String s : beanNamesForType) {
//            System.out.println(s);
//        }
//        DBHelper bean1 = run.getBean(DBHelper.class);
//        System.out.println(bean1);

        boolean user01 = run.containsBean("user01");
        System.out.println("容器中User组件:" + user01);

        boolean pet01 = run.containsBean("pet01");
        System.out.println("容器中Pet组件:" + pet01);


        boolean haha = run.containsBean("haha");
        boolean hehe = run.containsBean("hehe");
        System.out.println("haha:" + haha);
        System.out.println("hehe:" + hehe);
    }
}